package com.wujian.love.utils;

import com.alibaba.fastjson.JSON;
import com.wujian.love.contants.ClassContants;
import com.wujian.love.contants.RocketMQContants;
import com.wujian.love.domin.RocketMessage;
import com.wujian.love.mapper.RocketMessageMapper;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: MessageUtils
 * @description:
 * @author: wujian
 * @Date: 2020-07-07 10:28
 **/
@Component
public class MessageUtils {

    @Autowired
    private RocketMessageMapper rocketMessageMapper;

    private static MessageUtils messageUtils;

    @PostConstruct
    public void init() {
        messageUtils = this;
    }


    /**
     * @Author WuJian
     * @Description //创建延迟订单的消息Message
     * @Date 10:30 2020/7/7
     * @Param [params]
     * @return org.apache.rocketmq.common.message.Message
     **/
    public static Message createCancelOrderMessage(Map<String, Object> params){
        //自定义
        String keys = UUID.randomUUID().toString() + "-OrderParent-" + System.currentTimeMillis();
        //创建订单消息
        Message message = new Message();
        //设置延迟级别
        message.setDelayTimeLevel(RocketMQContants.ORDER_CANCEL_DELAY_LEVEL);
        //设置消息标签
        message.setTags(RocketMQContants.ORDER_CANCEL_TAG);
        //设置消息的主题
        message.setTopic(RocketMQContants.ORDER_TOPIC);
        message.setBody(JSON.toJSONString(params).getBytes());
        message.setKeys(keys);
        return message;
    }


    /**
     * @Author WuJian
     * @Description //创建订单扣减库存信息
     * @Date 10:30 2020/7/7
     * @Param [params]
     * @return org.apache.rocketmq.common.message.Message
     **/
    public static Message createStockOrderMessage(Map<String, Object> params){
        //自定义
        String keys = UUID.randomUUID().toString() + "-OrderParent-" + System.currentTimeMillis();
        //创建消息数据
        RocketMessage rocketMessage = new RocketMessage();
        rocketMessage.setConsumeFlag(RocketMessage.CONSUME_FlAG_FALSE);
        rocketMessage.setMessageKeys(keys);
        rocketMessage.setMessageTag(RocketMQContants.ORDER_STOCK_TAG);
        rocketMessage.setMessageTopic(RocketMQContants.ORDER_STOCK_TOPIC);
        rocketMessage.setDelFlag(ClassContants.NOT_DEL_FLAG);
        //通过从静态变量applicationContext中取得Bean获取mapper
        //RocketMessageMapper rocketMessageMapper = ContextUtils.getBean("RocketMessageMapper");
        //创建订单消息
        int row = messageUtils.rocketMessageMapper.insertRocketMessage(rocketMessage);
        Message message = new Message();
        if(row >0){
            //设置消息标签
            message.setTags(RocketMQContants.ORDER_STOCK_TAG);
            //设置消息的主题
            message.setTopic(RocketMQContants.ORDER_STOCK_TOPIC);
            message.setBody(JSON.toJSONString(params).getBytes());
            message.setKeys(keys);
        }
        return message;
    }

}
